﻿using System;
using System.ComponentModel;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Threading;
using HeritageAcademy.Window.Resources;

namespace HeritageAcademy.Shared
{
    public class DigitalTimer : StackPanel, INotifyPropertyChanged
    {
        #region Parameters

        DispatcherTimer _dispatcherTimer;
        TextBlock _hour, _minute, _second;
        int _hint = 0, _mint = 0, _sint = 0;
        double _fonSize = 10;

        #endregion

        #region Ctor

        public DigitalTimer()
        {
            InitializeDigitalTimer();
            InitalizeDigitalTimerView();
        }

        #endregion

        #region Properties

        public int Hour
        {
            get { return _hint; }
            set
            {
                _hint = value;
                if (_hint < 10)
                {
                    _hour.Text = "0" + _hint.ToString();
                }
                else
                {
                    _hour.Text = _hint.ToString();
                }
                OnPropertyChanged("Hour");
            }
        }

        public int Minute
        {
            get { return _mint; }
            set
            {
                _mint = value;
                if (_mint < 10)
                {
                    _minute.Text = "0" + _mint.ToString();
                }
                else
                {
                    _minute.Text = _mint.ToString();
                }
                OnPropertyChanged("Minute");
            }
        }

        public int Second
        {
            get { return _sint; }
            set
            {
                _sint = value;
                if (_sint < 10)
                {
                    _second.Text = "0" + _sint.ToString();
                }
                else
                {
                    _second.Text = _sint.ToString();
                }
                OnPropertyChanged("Second");
            }
        }

        public double FontSize
        {
            get { return _fonSize; }
            set 
            { 
                _fonSize = value;
                _hour.FontSize = _fonSize;
                _minute.FontSize = _fonSize;
                _second.FontSize = _fonSize;
                OnPropertyChanged("FontSize");
            }
        }

        #endregion

        #region Private Methods

        void InitializeDigitalTimer()
        {
            _dispatcherTimer = new DispatcherTimer();
            _dispatcherTimer.Tick += new EventHandler(digitalTimer_Tick);
            _dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
        }

        void InitalizeDigitalTimerView()
        {
            this.Orientation = System.Windows.Controls.Orientation.Horizontal;

            _hour = new TextBlock();
            _hour = AddTimeField("tbHour", "00");

            this.Children.Add(_hour);
            this.Children.Add(AddDelimeter());

            _minute = new TextBlock();
            _minute = AddTimeField("tbMinute", "00");

            this.Children.Add(_minute);
            this.Children.Add(AddDelimeter());

            _second = new TextBlock();
            _second = AddTimeField("tbSecond", "00");

            this.Children.Add(_second);
        }

        TextBlock AddTimeField(string name, string text)
        {
            TextBlock t = new TextBlock();
            t.Name = name;
            t.Background = new SolidColorBrush();
            t.Width = Double.NaN;
            t.Height = Double.NaN;
            t.VerticalAlignment = System.Windows.VerticalAlignment.Center;
            t.FontSize = _fonSize;
            t.Text = text;
            return t;
        }

        TextBlock AddDelimeter()
        {
            TextBlock t = new TextBlock();
            t.Background = new SolidColorBrush();
            t.Width = Double.NaN;
            t.Height = Double.NaN;
            t.Margin = new System.Windows.Thickness(4, 0, 4, 0);
            t.VerticalAlignment = System.Windows.VerticalAlignment.Center;
            t.FontSize = _fonSize;
            t.Text = Strings.Timer_Delimiter;
            return t;
        }

        void DropToInitialValue()
        {
            _sint = 0;
            _mint = 0;
            _hint = 0;
        }

        #endregion

        #region Public Methods

        public void Start()
        {
            DropToInitialValue();
            _dispatcherTimer.Start();
        }

        public void Stop()
        {
            _dispatcherTimer.Stop();
        }

        public void Restart()
        {
            //_dispatcherTimer.Stop();
            DropToInitialValue();
            //_dispatcherTimer.Start();
        }

        public override string ToString()
        {
            string time = String.Empty;
            time += _hint.ToString() + Strings.Timer_Delimiter + 
                    _mint.ToString() + Strings.Timer_Delimiter + 
                    _sint.ToString();
            return time;
        }

        #endregion

        #region Event Handlers

        private void digitalTimer_Tick(object sender, EventArgs e)
        {
            ++_sint;
            if (_sint >= 60)
            {
                _sint = 0;
                ++_mint;
            }
            
            if (_mint >= 60)
            {
                _sint = 0;
                _mint = 0;
                ++_hint;
            }

            Second = _sint;
            Minute = _mint;
            Hour = _hint;
        }

        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion
    }
}
